package root.electronclass.electronclass.personal.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.DialogPersonalGrowthBinding
import root.electronclass.electronclass.personal.activity.GrowthAdapter
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.CallbackNext
import root.electronclass.electronclass.mvp.base.data
import root.electronclass.electronclass.mvp.model.GrowthModel
import root.electronclass.electronclass.mvp.rest.RestManager
import retrofit2.Call

/**
 *@ClassName TODO
 *@Description TODO
 *@Author caofc
 *@Date 2023/10/26 22:33
 */
class PersonalGrowthDialog : DialogFragment() {

    var list1 = ArrayList<GrowthModel>()
    var list2 = ArrayList<GrowthModel>()
    var list3 = ArrayList<GrowthModel>()
    var list4 = ArrayList<GrowthModel>()
    var list5 = ArrayList<GrowthModel>()
    lateinit var binding: DialogPersonalGrowthBinding
    val adapter = GrowthAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.CustomDialog)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DialogPersonalGrowthBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        getData()
        binding.recyclerView.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
        binding.recyclerView.adapter = adapter
        binding.radioGroup2.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.button1) {
                adapter.refresh(list1)
            }
            if (checkedId == R.id.button2) {
                adapter.refresh(list2)
            }
            if (checkedId == R.id.button3) {
                adapter.refresh(list3)
            }
            if (checkedId == R.id.button4) {
                adapter.refresh(list4)
            }
            if (checkedId == R.id.button5) {
                adapter.refresh(list5)
            }
        }

        binding.close.setOnClickListener { dismiss() }
    }

    private fun getData() {
        RestManager.getInstance().initRest().space().enqueue(object : CallbackNext<BaseResponse<List<GrowthModel>>>(null) {
            override fun onSuccessful(call: Call<BaseResponse<List<GrowthModel>>>?, response: BaseResponse<List<GrowthModel>>) {
                val data = response.data
                list1.clear()
                list2.clear()
                list3.clear()
                list4.clear()
                list5.clear()
                data.forEach {
                    if (it.itemType == 1) {
                        list1.add(it)
                    }
                    if (it.itemType == 2) {
                        list2.add(it)
                    }
                    if (it.itemType == 3) {
                        list3.add(it)
                    }
                    if (it.itemType == 4) {
                        list4.add(it)
                    }
                    if (it.itemType == 5) {
                        list5.add(it)
                    }
                }
                adapter.refresh(list1)

            }

            override fun onError(call: Call<BaseResponse<List<GrowthModel>>>?, response: BaseResponse<List<GrowthModel>>?) {

            }

        })
    }

}